package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.RoleModel;
import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyMenuItem;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class UserRoleGridPanel extends LayoutContainer {
	
	private RoleRPCServiceAsync service = RoleRPCService.Util.getInstance();
	
	private UserMessages MESSAGES = UserMessages.INSTANCE;
	
	private Grid<RoleModel> roleGrid;
	private ListStore<RoleModel> gridStore;
	private GridSelectionModel<RoleModel> gridSelectionModel = new GridSelectionModel<RoleModel>();
	
	private SelectionListener<MenuEvent> mnuSelectListener = new SelectionListener<MenuEvent>() {
		@Override
		public void componentSelected(MenuEvent ce) {
			ce.getSource();
			MyMenuItem item = (MyMenuItem)ce.getItem();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private ModuleDetailGridDialog moduleDetailDialog;
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		add(getRoleGridPanel());
	}

	private Grid<RoleModel> getRoleGridPanel(){
		if(roleGrid == null){
			List<ColumnConfig> configs = RoleHelper.INSTANCE.getColumnConfigs();
			ColumnModel cm = new ColumnModel(configs);
			roleGrid = new Grid<RoleModel>(getGridStore(),cm);
			roleGrid.setSelectionModel(gridSelectionModel);
			roleGrid.setContextMenu(RoleHelper.INSTANCE.getParentRoleGridContextMenu(mnuSelectListener));
		}
		return roleGrid;
	}
	
	public ListStore<RoleModel> getGridStore(){
		if(gridStore == null){
			gridStore = new ListStore<RoleModel>();
			gridStore.setModelComparer(RoleHelper.INSTANCE.getModelComparer());
			gridStore.setKeyProvider(RoleHelper.INSTANCE.getModelKeyProvider());
		}
		return gridStore;
	}
	
	public ModuleDetailGridDialog getModuleDetailDialog(){
		if(moduleDetailDialog==null){
			moduleDetailDialog = new ModuleDetailGridDialog();
		}
		return moduleDetailDialog;
	}

	private void onBtnMnuEvent(BaseAction action) {
		if(action == RoleStateMachine.SHOW_SELFMODULES_ACTION){
			onShowSelfModules();
		}
		else if(action == RoleStateMachine.SHOW_ALLMODULES_ACTION){
			onShowAllModules();
		}
	}
	
	private void onShowAllModules() {
		List<RoleModel> modules = gridSelectionModel.getSelectedItems();
		if(modules == null||modules.size()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_showdetail_module(), null);
			return;
		}
		
		GXTHelper.maskQuery(this);
		service.getAllModulesOfRole(modules.get(0), new GwtAsyncCallback<List<ModuleModel>>(this) {
			@Override
			public void doSuccess(List<ModuleModel> result) {
				getModuleDetailDialog().showDialog(result);
			}
		});
	}

	private void onShowSelfModules() {
		List<RoleModel> models = gridSelectionModel.getSelectedItems();
		if(models == null||models.size()==0){
			MessageBox.alert(MESSAGES.msgbox_alert_title(), MESSAGES.msg_alert_showdetail_module(), null);
			return;
		}
		final RoleModel model = models.get(0);
		List<ModuleModel> moduleModels = model.getAuthedModules();
		if(moduleModels!=null&&moduleModels.size()>0){
			getModuleDetailDialog().showDialog(moduleModels);
			return;
		}
		GXTHelper.maskQuery(this);
		service.getRoleSelfModules(model, new GwtAsyncCallback<List<ModuleModel>>(this) {
			@Override
			public void doSuccess(List<ModuleModel> result) {
				model.setAuthedModules(result);
				getModuleDetailDialog().showDialog(result);
			}
		});
		
	}
	
	public void removeAllData(){
		getGridStore().removeAll();
	}
	
	public void addGridValues(List<RoleModel> models){
		if(models == null || models.isEmpty()){
			return;
		}
		getGridStore().add(models);
	}
	
	public void setGridValues(List<RoleModel> models){
		removeAllData();
		if(models!=null&&models.size()>0){
			getGridStore().add(models);
			List<RoleModel> ums = new ArrayList<RoleModel>(1);
			ums.add(models.get(0));
			gridSelectionModel.setSelection(ums);
		}
	}
}
